﻿using RestSharp;
using System.Data;

namespace VSTODemo {
    public class SapTableService {
        private string baseUrl = "http://localhost:5000";
        private RestClient restClient;

        public SapTableService() {
            restClient = new RestClient(baseUrl);
        }

        public DataTable GetTableFields(string tableName) {
            DataTable result = null;

            var url = baseUrl + "/tablefields/" + tableName;
            var request = new RestRequest(url, Method.GET);
            var resp = restClient.Execute(request);
            if (resp.IsSuccessful) {
                result = JsonUtils.ToTable(resp.Content);
            }

            return result;
        }

        public DataTable GetTableContent(string tableName, string criteria, string rows) {
            DataTable result = null;

            string url = null;
            if (string.IsNullOrEmpty(criteria)) {
                url = baseUrl + $"/tabledata/{tableName}/{rows}";
            }
            else {
                url = baseUrl + $"/tabledata/{tableName}/{rows}?options={criteria}";
            }
            

            var request = new RestRequest(url, Method.GET);
            var resp = restClient.Execute(request);
            if (resp.IsSuccessful) {
                result = JsonUtils.ToTable(resp.Content);
            }

            return result;
        }
    }
}
